package com.tarena.lbs.business.service.impl;


import com.tarena.lbs.base.common.utils.Asserts;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.business.api.pojo.BusinessDto;
import com.tarena.lbs.business.dao.repository.BusinessRepository;
import com.tarena.lbs.business.dao.repository.StoreRepository;
import com.tarena.lbs.business.pojo.param.BusinessParam;
import com.tarena.lbs.business.pojo.po.BusinessPO;
import com.tarena.lbs.business.pojo.query.BusinessQuery;
import com.tarena.lbs.business.pojo.vo.BusinessStoreVO;
import com.tarena.lbs.business.pojo.vo.BusinessVO;
import com.tarena.lbs.business.service.BusinessService;
import com.tarena.lbs.common.enums.PassportResultEnum;
import com.tarena.lbs.common.enums.Roles;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
@Slf4j
@Service
public class BusinessServiceImpl implements BusinessService {

    @Autowired
    private BusinessRepository  businessRepository;
    @Autowired
    private StoreRepository storeRepository;


    @Override
    public PageResult<BusinessVO> pageList(BusinessQuery query) {

        PageResult<BusinessVO> pageResult = businessRepository.selectPageList(query);
        return pageResult;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(BusinessParam param) throws BusinessException {
        log.debug("正在保存商家信息:{}",param);
     //1.检查当前登录用户的角色是否符合我当前业务的角色要求  ADMIN
        checkRole(Roles.ADMIN);
        //2.验证检查幂等 是否存在相同名称的商家数据
        BusinessDto businessDto = businessRepository.getBusinessByName(param.getBusinessName());
        log.debug("查询到的商家信息:{}",businessDto);
        //3.幂等验证正常 可以新增 封装 数据对象PO 执行save新增
        if (businessDto!=null){
            throw new BusinessException("-2", "商家名称重复");
        }else {//4.新增
            BusinessPO businessPO =new BusinessPO();
            businessPO.setEntryTime(new Date());
            BeanUtils.copyProperties(param,businessPO);
            businessRepository.save(businessPO);
        }
    }

    private void checkRole(Roles roles) {
        if (roles!=Roles.ADMIN){
            throw new RuntimeException("当前用户角色不匹配");
        }
    }

    @Override
    public BusinessStoreVO busiStoreDetail(Integer businessId) {
        log.debug("正在查询商家详情:{}",businessId);
        BusinessStoreVO businessStoreVO = businessRepository.findById(businessId);
       businessStoreVO.setStoreVOList(storeRepository.findByBusinessId(businessId));
        return businessStoreVO;
    }

    //查询商家详情API

    @Override
    public BusinessDto getBusinessById(Integer businessId) {
        log.debug("正在查询商家详情:{}",businessId);
        BusinessDto businessDto = businessRepository.getBusinesById(businessId);

        return businessDto;
    }

    @Override
    public void updateStatus(BusinessParam param) {
        log.info("正在更新商家状态:{}",param);
        BusinessPO businessPO = new BusinessPO();
        BeanUtils.copyProperties(param,businessPO);
        businessRepository.updateStatus(param);

    }
}
